/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/**
 *
 * $Id: IMUServer.hpp,v 1.5 2009-09-23 11:56:14 morsko Exp $
 *
 * \file
 * \brief MaCI - IMU Server interface
 * \author Matthieu Myrsky <matthieu.myrsky@tkk.fi>
 */
#ifndef _MACI_INTERFACE_IMUSERVER_HPP_
#define _MACI_INTERFACE_IMUSERVER_HPP_

#include "MaCI.hpp"
#include "IMUData.hpp"
#include "thread.hpp"
#include "sync.hpp"
#include <list>

// Forward declare gimi::GIMI
namespace gimi {
  class GIMI;
  class GIMIMessage;
}


namespace MaCI{
  namespace IMU{

    /** MaCI - IMU Server interface
     * 
     * This class contains the methods and types for providing
     * the IMU service in the GIMnet.
     */
    class CIMUServer : public MaCI::CMaCI,
                       private gim::CThread,
                       private gim::CSync
    {
    public:

      /** Constructor.
       * 
       * Constructor of the IMUServer. This requires;
       * a pointer to Initialized and connected GIMI, minor
       * number of this interface (Used as GIMI minor number)
       *
       * @param[in] aMaCICtrlServerPtr Pointer to GIMI used for communication
       * @param[in] aInterfaceMinor    Interface minor number. Usually used to 
       *                               indicate the instance of devices of same
       *                               type.
       * @param[in] aDescription       Description of the server
       */
      CIMUServer(MaCICtrl::CMaCICtrlServer *aMaCICtrlServerPtr, 
                 const int aInterfaceMinor,
                 const std::string &aDescription = "");

      /** Destructor.
       */
      ~CIMUServer();

      /** Opens IMU Server
       *
       * Opens the IMU Server,  basicly registers device to GIMI.
       *  
       * NOTE: You must set event types before opening!!
       * @return                Returns a value of type EMaCIError. 
       *                        
       * \see EMaCIError
       */
      MaCI::EMaCIError DoOpen(void);

      /** Close device.
       *
       * Closes the device. Closes all GIMI registrations and queues.
       *
       * @return                Returns a value of type EMaCIError.
       *
       * \see EMaCIError
       */
      MaCI::EMaCIError DoClose(void);

      /** Wait for a Command.
       *
       * This function waits until new command arrives or timeout occurs.
       * If there is unhandled commands since last read, the next
       * in queue is returned. If you want to clear the queue between calls,
       * use ClearCommandBuffer() function.
       *
       * When a command is succesfully returned, the user can inspect
       * the current command through the GetCommand() call of
       * class CIMUData. Then, according to command and its
       * specification, additional datafields may be present, they
       * can be queried by similar Get... functions. See the 
       * description of the data encoder/decoder module CIMUData.
       *
       * 
       * \see ClearCommandBuffer()
       * \see CIMUData
       * \see MaCI::EMaCIError
       * 
       * @param[out] aData      Reference to IMUData. This will
       *                        be filled with incoming command when
       *                        this call is completed succesfully.
       * @param[out] aInfo      Reference to replyInfo. Use this replyInfo 
       *                        to SendReply-function when you have to send 
       *                        a Reply.
       * @param[in] aTimeout_ms Timeout to wait until operation fails
       *                        for timeout.
       * @param[in] aSkipQueued If this value is set to 'true', the
       *                        function will only return the last
       *                        received command and pop older ones
       *                        out from the queue. If this is set to
       *                        'false', all commands are received in
       *                        order. If parameter is undefined, 
       *                        default is 'false'.
       *
       * @return                'KMaCIOK'  when succesfully received
       *                        a new command. KMaCITimeout when operation
       *                        timed out before a command was received.
       *                        Other errors returned when appropriate.
       *                        (See the MaCI::EMaCIError for description)
       * \see EMaCIError
       */
      MaCI::EMaCIError WaitCommand(CIMUData &aData, 
				   TReplyInfo &aInfo,
                                   const unsigned int aTimeout_ms,
                                   bool aSkipQueued = false);
      /**
       * Sends IMU event to service subscribers.
       * The CIMUData must contain  KCommandEvent-Command and the structures
       * set at the SetEventTypes-function
       *
       * @param[in] aData         CIOData container containing KCommandEvent-Command 
       *                          and the structures set at the SetEventTypes-function
       *                          
       */
      bool SendEvent(const CIMUData &aData);

      /** Sends reply message to client
       * 
       * This function sends synchronous reply message to client.
       *
       * @param[in] aData       Reference to IMUData. This data will be sent to
       *                        client for reply
       * @param[in] aInfo       Reference to replyInfo. Get this replyinfo from
       *                        WaitCommand.
       * @return                'KMaCIOK' when reply message succesfully sent.
       *                        Other errors returned when appropriate.
       *                        (See the MaCI::EMaCIError for description)
       * \see EMaCIError
       */
      MaCI::EMaCIError SendReply(CIMUData &aData,
				 TReplyInfo &aInfo);

      /**
       * Set the types which are sent by event. you must set this before opening the
       * Server
       *
       * @param[in] aType       Reference to TEventTypes. This data will be set, and
       *                        client will ask it when connecting to server.
       * @return                true if success, false if fails
       */
      bool SetEventTypes(TEventTypes &aType);
     
    private:
      int ThreadFunction(const int aThreadNumber); ///< Incoming data handler function
      int ProcessMessage(const gimi::GIMIMessage &msg); ///< Incoming data processor function
      std::string iServiceDescription; ///< Service description.
      volatile bool iServerActive; ///< Flag indicating whether the server is active.
      TEventTypes iEventTypes;
      bool iEventTypesSet;
      std::list<CIMUData *> iIMUDataArray; ///< Array of unprocessed commands.
      std::list<TReplyInfo> iReplyDataArray;


    };

  }

}

#endif ///< _MaCI_INTERFACE_IMUSERVER_HPP_
